package com.example.coursedesign.ui.notifications;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.coursedesign.MainActivity;
import com.example.coursedesign.R;
import com.example.coursedesign.ui.notifications.adapter.FamilyMemberAdapter;
import com.example.coursedesign.ui.notifications.entity.FamilyMember;

import java.util.ArrayList;
import java.util.List;

public class MonitoringActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private FamilyMemberAdapter adapter;
    private List<FamilyMember> familyMembers = new ArrayList<>();
    private Button buttonAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.monitoring);

        recyclerView = findViewById(R.id.recyclerView);
        buttonAdd = findViewById(R.id.button_add);

        adapter = new FamilyMemberAdapter(this, familyMembers);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        // 启用返回按钮
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        buttonAdd.setOnClickListener(v -> {
            Intent intent = new Intent(MonitoringActivity.this, AddFamilyMemberActivity.class);
            startActivityForResult(intent, 1); // 使用 startActivityForResult 以便获取返回的数据
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
            FamilyMember newMember = data.getParcelableExtra("new_member");
            familyMembers.add(newMember);
            adapter.notifyItemInserted(familyMembers.size() - 1);
        }
    }
    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed(); // 返回上一个 Activity
        return true;
    }
}